//
//  viewLogingView.swift
//  我的微博练习
//
//  Created by zhangguo on 15/10/9.
//  Copyright © 2015年 zhangguo. All rights reserved.
//

import UIKit

//访客视图协议
protocol VisitorLoginViewDelegate: NSObjectProtocol {
    
    func visitorLoginViewWillLogin()
    
    func visitorLoginViewWillRegister()
    
}
class viewLogingView: UIView {
// MARK: - 定义代理
    weak var delegate:VisitorLoginViewDelegate?
    func clickLogin(){
        delegate?.visitorLoginViewWillLogin()
    }
    
    func clickRegister(){
        delegate?.visitorLoginViewWillRegister()
    }
    
    //MARK: - 界面初始化
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }
    
     //MARK: -设置视图信息
    func setUpViewInfor(ishome:Bool,imageName:String,message:String){
        
        messageLabe.text = message
        iconview.image = UIImage(named: imageName)
        
        homeview.hidden = !ishome
        
        //判断是否是小房子
        ishome ? startAnimation()  :sendSubviewToBack(maskIconView)
        
    }
    
//开始动画
    private func startAnimation(){
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 20.0
        //提示；如果界面上有动画，一定要检查退出动画再进入动画是否还在。
        anim.removedOnCompletion = false
        
        iconview.layer.addAnimation(anim, forKey: nil)
    }
    
    required init?(coder aDecoder: NSCoder) {
//        fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
        
        setUpUI()
    }
    
    //添加 控件
   private func setUpUI(){
    
    addSubview(iconview)
    addSubview(maskIconView)
    addSubview(homeview)
    addSubview(messageLabe)
    addSubview(logingButton)
    addSubview(registerButton)

    //自动布局
    //如果使用自动布局需要设置这个属性，不要自己设置frame
    iconview.translatesAutoresizingMaskIntoConstraints = false
    
    addConstraint(NSLayoutConstraint(item: iconview, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
    
     addConstraint(NSLayoutConstraint(item: iconview, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: -60))
    
//    小房子
    homeview.translatesAutoresizingMaskIntoConstraints = false
    addConstraint(NSLayoutConstraint(item: homeview, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconview, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
    addConstraint(NSLayoutConstraint(item: homeview, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconview, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
    
    //    消息 文字
    messageLabe.translatesAutoresizingMaskIntoConstraints = false
    addConstraint(NSLayoutConstraint(item: messageLabe, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconview, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
    
    addConstraint(NSLayoutConstraint(item: messageLabe, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconview, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
    
       addConstraint(NSLayoutConstraint(item: messageLabe, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 224))
    
     //   登陆
    logingButton.translatesAutoresizingMaskIntoConstraints = false
    addConstraint(NSLayoutConstraint(item: logingButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: messageLabe, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: 0))
    
    addConstraint(NSLayoutConstraint(item: logingButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabe, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
    
    addConstraint(NSLayoutConstraint(item: logingButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.Width, multiplier: 1.0, constant: 100))
    
    addConstraint(NSLayoutConstraint(item: logingButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.Height, multiplier: 1.0, constant: 35))
    
    //    注册
    
    registerButton.translatesAutoresizingMaskIntoConstraints = false
    addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: messageLabe, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: 0))
    
    addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabe, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
    
    addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.Width, multiplier: 1.0, constant: 100))
    
    addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.Height, multiplier: 1.0, constant: 35))
    
    //    遮罩视图
    maskIconView.translatesAutoresizingMaskIntoConstraints = false
    addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[subview]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views:["subview":maskIconView]))
    addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[subview]-(-35)-[reginButton]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views:["subview":maskIconView,"reginButton":registerButton]))
    
    //    背景颜色
    backgroundColor = UIColor(white: 237.0/255.0 , alpha: 1.0)
    
    }
    
    //MARK:-L懒加载控件  图标
   private lazy var iconview:UIImageView = {
        let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        return iv
    }()
    
    //房子控件
    private lazy var homeview:UIImageView = {
        let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        return iv
    }()
    
     //遮造视图
    private lazy var maskIconView:UIImageView = {
        
        let mask = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        return mask
    }()
    
    //消息控件
    private lazy var messageLabe:UILabel = {
        
        let label = UILabel()
        
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        label.font = UIFont.systemFontOfSize(14)
        label.tintColor = UIColor.darkGrayColor()
        label.numberOfLines = 0
        
        label.sizeToFit()
        
        return label
    }()
    
    //登陆按钮
    private lazy var logingButton:UIButton = {
        
        let button = UIButton()
        
        button.setTitle("登陆", forState: UIControlState.Normal)
        
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        //
        button.addTarget(self, action: "clickLogin", forControlEvents: UIControlEvents.TouchUpInside)
        
        
        return button
        }()

    
      //注册控件
    private lazy var registerButton:UIButton = {
        let reginteButton = UIButton()
        reginteButton.setTitle("注册", forState: UIControlState.Normal)
        
        reginteButton.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        reginteButton.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
       
        reginteButton.addTarget(self, action: "clickRegister", forControlEvents: UIControlEvents.TouchUpInside)
        
        return reginteButton
    }()
    
}
